package demo;

import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import top.lingkang.finalgateway.utils.SslUtils;

import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLSession;
import java.io.File;
import java.io.FileInputStream;
import java.nio.file.Files;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Arrays;

/**
 * @author lingkang
 * @create by 2024/5/14 9:26
 */
public class Demo12 {
    public static void main(String[] args) throws Exception {

        SslContextBuilder forServer = SslContextBuilder.forServer(
                new File("C:\\Users\\Administrator\\Desktop\\temp\\key\\fullchain.pem"),
                new File("C:\\Users\\Administrator\\Desktop\\temp\\key\\privkey_pkcs8.pem"),
                null
        );
        SslContext sslContext = forServer.build();
        SSLEngine engine = sslContext.newEngine(null);
        engine.setEnabledProtocols(new String[]{"TLSv1.3"});
        System.out
                .println("支持的协议: " + Arrays.asList(engine.getSupportedProtocols()));
        System.out.println("启用的协议: " + Arrays.asList(engine.getEnabledProtocols()));
        System.out.println("支持的加密套件: "
                + Arrays.asList(engine.getSupportedCipherSuites()));
        System.out.println("启用的加密套件: "
                + Arrays.asList(engine.getEnabledCipherSuites()));

        CertificateFactory factory = CertificateFactory.getInstance("X.509");
        Certificate cert = factory.generateCertificate(Files.newInputStream(new File("C:\\Users\\Administrator\\Desktop\\temp\\key\\fullchain.pem").toPath()));

        X509Certificate x509Cert = (X509Certificate) cert;

        // 输出证书信息
        System.out.println("Subject: " + x509Cert.getSubjectDN().getName());
        System.out.println("Issuer: " + x509Cert.getIssuerDN().getName());
        System.out.println("Not Before: " + x509Cert.getNotBefore());
        System.out.println("Not After: " + x509Cert.getNotAfter());
    }
}
